package com.ssyc.gsk_teacher.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.makeramen.roundedimageview.RoundedImageView;
import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.base.Constants;
import com.ssyc.common.glide.ImageUtil;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.PopUpManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.MathUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.common.view.ZzHorizontalProgressBar;
import com.ssyc.common.view.dialog.CustomDialogManager;
import com.ssyc.gsk_teacher.R;
import com.ssyc.gsk_teacher.bean.PetInfo;
import com.ssyc.gsk_teacher.bean.PxbInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 排行榜--经验值--列表适配器
 * Created by Administrator on 2018/7/4 0004.
 */

public class EmpircalRvAdapter extends BaseQuickAdapter<PxbInfo.DataBean.LISTBean, BaseViewHolder> {

    private Context context;
    private Activity activity;

    public EmpircalRvAdapter(Activity activity, Context context, int layoutResId, @Nullable List<PxbInfo.DataBean.LISTBean> data) {
        super(layoutResId, data);
        this.context = context;
        this.activity = activity;
    }

    @Override
    protected void convert(BaseViewHolder helper, PxbInfo.DataBean.LISTBean item) {

        //显示1,2,3位的图标标记,只有条目位置在123时显示
        ImageView ivShowPos = helper.getView(R.id.iv_show_pos);

        //其它位置的文字标识
        TextView tvShowPos = helper.getView(R.id.tv_show_pos);

        if (helper.getLayoutPosition() == 0) {
            ivShowPos.setVisibility(View.VISIBLE);
            tvShowPos.setVisibility(View.GONE);
            ivShowPos.setImageResource(R.drawable.teacher_one);
        } else if (helper.getLayoutPosition() == 1) {
            ivShowPos.setVisibility(View.VISIBLE);
            tvShowPos.setVisibility(View.GONE);
            ivShowPos.setImageResource(R.drawable.teacher_two);
        } else if (helper.getLayoutPosition() == 2) {
            ivShowPos.setVisibility(View.VISIBLE);
            tvShowPos.setVisibility(View.GONE);
            ivShowPos.setImageResource(R.drawable.teacher_three);
        } else {
            ivShowPos.setVisibility(View.GONE);
            tvShowPos.setVisibility(View.VISIBLE);
            tvShowPos.setText((helper.getLayoutPosition() + 1) + "");
        }

        //头像
        RoundedImageView ivHead = helper.getView(R.id.iv_head);

        ImageUtil.displayImage(context, ivHead, item.getSTUDENT_ICON(), R.drawable.defaultimage);

        //姓名
        TextView tvName = helper.getView(R.id.tv_name);
        tvName.setText(item.getSTUDENT_NAME());

        //经验值
        TextView tvPresent = helper.getView(R.id.tv_size);
        tvPresent.setText(item.getEXP() + "");

        //详情
        Button btDetails = helper.getView(R.id.bt_details);
        btDetails.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                httpPetDetails(item);
            }
        });
    }

    /**
     * 弹出显示宠物的PopUpWindow
     *
     * @param item
     */
    private void httpPetDetails(PxbInfo.DataBean.LISTBean item) {
        CustomDialogManager.show(context, "加载中...");
        //请求宠物的数据
        Map<String, String> map = new HashMap<>();
        map.put("type", "50");
        map.put("acc", item.getUSER_ID() + "");
        map.put("role", AccountUtils.getRole(context));
        map.put("rowId", item.getROW_ID() + "");
        map.put("apptoken", AccountUtils.getTestToken(context));
        map.put("platform", "8");
        HttpUtils.post(HttpAdress.PETDETAILS, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                CustomDialogManager.dissmiss();
                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {
                CustomDialogManager.dissmiss();
                if (!TextUtils.isEmpty(response)) {
                    PetInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, PetInfo.class);
                    } catch (Exception e) {
                        Log.i("test", Constants.PARSEDATAERROR);
                    }
                    if (null != info) {
                        if ("200".equals(info.getState())) {
                            PetInfo.DataBean data = info.getData();
                            //返回成功
                            if (data != null) {
                                ShowPetPop(data);
                            }

                        } else {
                            UiUtils.Toast(Constants.ERRORSTATE + info.getState(), false);
                            Log.i("test", Constants.ERRORSTATE + info.getState());
                        }
                    } else {
                        Log.i("test", Constants.EMPTY);
                    }

                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }
        });
    }

    /**
     * 弹框显示宠物详情
     *
     * @param info
     */
    private void ShowPetPop(PetInfo.DataBean info) {

        PopUpManager.showPop(context, R.layout.teacher_pop_pxb_petinfo, 0.3f, activity, new PopUpManager.onGetViewListener() {
            @Override
            public void getChildView(View view, int layoutResId) {
                //关闭
                ImageView ivClose = view.findViewById(R.id.lv_close);
                ivClose.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        PopUpManager.dismiss();
                    }
                });

                //头像
                RoundedImageView ivHead = view.findViewById(R.id.lv_pet);
                if(info.getPET_ID()==1){
                    ivHead.setImageResource(R.drawable.base_pet_icon1);
                }else if(info.getPET_ID()==2){
                    ivHead.setImageResource(R.drawable.base_pet_icon2);
                }else if(info.getPET_ID()==3){
                    ivHead.setImageResource(R.drawable.base_pet_icon3);
                }else if(info.getPET_ID()==4){
                    ivHead.setImageResource(R.drawable.base_pet_icon4);
                }
//                ImageUtil.displayImage(context, ivHead, info.getICON(), R.drawable.defaultimage);

                //等级
                TextView tvLerver = view.findViewById(R.id.tv_lerver);
                tvLerver.setText("LV." + info.getLEVEL());

                //做45度的旋转动画
                RotateAnimation rotate = new RotateAnimation(0f, -45f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                LinearInterpolator lin = new LinearInterpolator();
                rotate.setInterpolator(lin);
                rotate.setDuration(10);
                rotate.setFillAfter(true);
                tvLerver.setAnimation(rotate);

                //昵称
                TextView tvPetName = view.findViewById(R.id.tv_pet_name);
                tvPetName.setText(info.getNICK_NAME());

                //性别
                TextView tvSex = view.findViewById(R.id.tv_pet_sex);
                if (info.getGENDER() == 0) {
                    //男
                    tvSex.setText("女");
                } else if (info.getGENDER() == 1) {
                    tvSex.setText("男");
                }

                //生日
                TextView tvBirthday = view.findViewById(R.id.tv_pet_birthday);
                tvBirthday.setText(info.getCREATE_TIME());

                //进度条
                ZzHorizontalProgressBar pbVigor = view.findViewById(R.id.pb_vigor);
                pbVigor.setBgColor(context.getResources().getColor(R.color.pb_bg));
                pbVigor.setProgressColor(context.getResources().getColor(R.color.pb_progress_color));
                pbVigor.setProgress((int) (MathUtil.deciMal(info.getEXP(), info.getNEXT_EXP()) * 100));

                //显示进度条的文字
                TextView tvPbNum = view.findViewById(R.id.tv_pb_num);
                tvPbNum.setText(info.getEXP() + "/" + info.getNEXT_EXP());
                //属性
                TextView tvZhi = view.findViewById(R.id.tv_zhi);
                TextView tvYong = view.findViewById(R.id.tv_yong);
                TextView tvHuan = view.findViewById(R.id.tv_huan);
                TextView tvAi = view.findViewById(R.id.tv_ai);
                tvZhi.setText(info.getWISDOM() + "");
                tvYong.setText(info.getBRAVE() + "");
                tvHuan.setText(info.getJOY() + "");
                tvAi.setText(info.getLOVE() + "");

            }
        });
    }
}
